$(document).ready(function() {
	setTimeout("init();",500) ;
}) ;

function init(){
	$("#memberLayoutAll").extcloudTabChange($("#memberLayoutAll .ui-tabs-nav > li"),$("#changeUser > ul") ,null) ;
    // all 为全部  department 部门 defaults 当前部门
	
	var treeChoose = $.fn.zTree.getZTreeObj("choose");
	var treechoosedte = treeChoose.getNodes();
	$.fn.zTree.init($("#choose"), settingchoose ,treechoosedte);
}

var setting = {  
		async:{
			enable: true,
			url:  getUrl
		},
		check:{
			chkStyle:"checkbox",
			chkboxType  :{ "Y" : "s", "N" : "ps" },  // 点击复选框节点之间的选择
			enable: true
			
		},
		view:{
			showIcon: true ,// 是否显示节点图标
			expandSpeed: "slow" // 节点展开效果
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onRightClick: onRightClick , // 屏蔽右键
			beforeDrag: beforeDrag,
			beforeDrop: beforeDropRealize ,
			beforeExpand: beforeExpand,
			onAsyncSuccess: onAsyncSuccess
		},
		edit: {
			enable: true ,
			showRemoveBtn: false,
			showRenameBtn: false
		}
	}; 

var settingchoose = {  
		async:{
			enable :false
		},
		view:{
			showLine: false,
			showIcon: true ,
			expandSpeed: "slow" 
		},
		callback: {
			onRightClick: onRightClick ,
			onDblClick: beforeRemove
		},
		edit: {
			enable: true ,
			showRenameBtn : false,
			removeTitle: "点击删除节点"
		}
	}; 

function onChoose(){
	var mycars=new Array("ec_current_unit_defaults_tree","ec_department_tree","ec_possessions_all_tree") ;
	var treeChoose = $.fn.zTree.getZTreeObj("choose");
	for ( var j = 0; j < mycars.length; j++) {
		var treeObj = $.fn.zTree.getZTreeObj(mycars[j]);
		if(treeObj != null){
		    var nodes = treeObj.getCheckedNodes(true);
			for(var i = 0 ; i < nodes.length ; i ++) {
				var nodesvalue=nodes[i].id;
				var nodesChoose = treeChoose.getNodesByParam("id", nodesvalue, null);
				if(nodes[i].isParent){
					getData(nodesvalue ,mycars[j]);
					if(choosedata.length >50){
						Boxy.confirm("您选择的数据过长是否继续执行！", function() {
							for(var j = 0 ; j < choosedata.length ; j ++){
								var Choose = treeChoose.getNodesByParam("id", choosedata[j].id, null);
								if(Choose=="" ){
									treeChoose.addNodes(null, choosedata[j]);
								}
							}
						}, {unloadOnHide:true, modal: false,cache:true, title:'提示信息',clone:true});
					}else{
						for(var j = 0 ; j < choosedata.length ; j ++){
							var Choose = treeChoose.getNodesByParam("id", choosedata[j].id, null);
							if(Choose=="" ){
								treeChoose.addNodes(null, choosedata[j]);
							}
						}
					}
				}else{
					var Choose = treeChoose.getNodesByParam("id", nodes[i].id, null);
					if(Choose=="" ){
						treeChoose.addNodes(null, nodes[i]);
					}
				}
		    }
		}
	}
}
function onRightClick(event, treeId, treeNode) {

};
var choosedata ;
function beforeDrag(treeId, treeNodes) {
	if(treeId=="choose"){
		return false ;
	}
	var treechose = $.fn.zTree.getZTreeObj("choose");
	for(var i = 0 ; i < treeNodes.length ; i ++) {
		var nodesvalue=treeNodes[i].id;
		var nodesChoose = treechose.getNodesByParam("id", nodesvalue, null);
		if(treeId =="ec_department_tree"){
			if(nodesChoose=="" ){
				var treenodesdate = treeNodes[i] ;
				treenodesdate.children=null ;
				if(treenodesdate.isParent){
					treenodesdate.isParent=false ;
				}
				treechose.addNodes(null, treenodesdate);
				if(!treenodesdate.isParent){
					treenodesdate.isParent=true ;
				}
			}
		}else{
			if(nodesChoose=="" && !treeNodes[i].isParent  ){
				treechose.addNodes(null, treeNodes[i]);
			}else{
				getData(nodesvalue , treeId);
				if(choosedata.length >50){
					Boxy.confirm("您选择的数据过长是否继续执行！", function() {
						for(var j = 0 ; j < choosedata.length ; j ++){
							var Choose = treechose.getNodesByParam("id", choosedata[j].id, null);
							if(Choose=="" ){
								treechose.addNodes(null, choosedata[j]);
							}
						}
					}, {unloadOnHide:true, modal: false,cache:true, title:'提示信息',clone:true});
				}else{
					for(var j = 0 ; j < choosedata.length ; j ++){
						var Choose = treechose.getNodesByParam("id", choosedata[j].id, null);
						if(Choose=="" ){
							treechose.addNodes(null, choosedata[j]);
						}
					}
				}
				
			}
		}
	}
	return false;
}

function getData(id , type){
	$.ajax({  
	       async : false,  
	       cache:false,  
	       type: 'POST',  
	       data: "type="+type+"&id="+id ,  // all 为全部 department 部门 defaults 当前部门
	       dataType : "json",  
	       url: context+"/oa/ecTreeAction.do?method=getchooseZTreedata",
	       error: function () { 
	           alert('数据加载失败！');  
	       },  
	       success:function(data){  
	    	   choosedata = data;    
	       }  
	   }); 
}

function beforeDropRealize(treeId, treeNodes) {
	return false;
}

function addNodes(TreeObj, treeNodes) {
//	var nodesvalue=treeNodes.children;
//	if(nodesvalue!=null){
//		for(var j = 0 ; j < nodesvalue.length ;j ++){
//			if(nodesvalue[j].children!=null){
//				addNodes(treeId, nodesvalue[j]);
//			}else{
//				var nodes = TreeObj.getNodesByParam("id", nodesvalue[j].id, null);
//				if(nodes==""){
//					TreeObj.addNodes(null, nodesvalue[j]);
//				}
//			}
//		}
//   }
}

function beforeRemove() {
	var treeRemove = $.fn.zTree.getZTreeObj("choose");
	var selectnode = treeRemove.getSelectedNodes();
	for (var i=0, l=selectnode.length; i < l; i++) {
		treeRemove.removeNode(selectnode[i]);
	}
}

function deleteAll(){
	$.fn.zTree.init($("#choose"), settingchoose, "");
}
function loaddate(type){
		$.fn.zTree.init($("#"+type), setting );
}


function onAsyncSuccess(event, treeId, treeNode, msg) {
		var zTree = $.fn.zTree.getZTreeObj(treeId);
		zTree.updateNode(treeNode);
}
function beforeExpand(treeId, treeNode) {
	if (!treeNode.isAjaxing) {
		ajaxGetNodes(treeNode, "refresh" ,treeId);
		return true;
	} else {
		return false;
	}
}
function ajaxGetNodes(treeNode, reloadType ,treeId ) {
	var zTree = $.fn.zTree.getZTreeObj(treeId);
	if (reloadType == "refresh") {
		zTree.updateNode(treeNode);
	}
	zTree.reAsyncChildNodes(treeNode, reloadType, true);
}

function getUrl(treeId, treeNode) {
	var param ="" ;
	var treetype =treeId.split("_") ;
	if(treetype.length >2){
		param= "type="+treetype[treetype.length-2];
	}
	if(treeNode!=null){
		param = param +"&id="+treeNode.id ;
	}
	url = context+'/oa/ecTreeAction.do?method=getZTreedata&'+param ;
	return url ;
}

